package com.sgg.datastructure.tree;

public class ArrayBinaryTree {
    public static int[] arr = {1, 2, 3, 4, 5, 6, 7};

    public static void main(String[] args) {
//        preOrder(); // 1,2,4,5,3,6,7
//        middleOrder();//4 2 5 1 6 3 7
        postOrder();// 4 5 2 6 7 3 1
    }

    public static void preOrder() {
        preOrder(0);
    }

    public static void preOrder(int index) {
        System.out.println(arr[index]);
        if (2 * index + 1 < arr.length) {
            preOrder(2 * index + 1);
        }
        if (2 * index + 2 < arr.length) {
            preOrder(2 * index + 2);
        }
    }
    public static void middleOrder() {
        middleOrder(0);
    }

    private static void middleOrder(int index) {
        if (2 * index + 1 < arr.length) {
            middleOrder(2 * index + 1);
        }
        System.out.println(arr[index]);
        if (2 * index + 2 < arr.length) {
            middleOrder(2 * index + 2);
        }
    }

    public static void postOrder() {
        postOrder(0);
    }

    private static void postOrder(int index) {
        if (2 * index + 1 < arr.length) {
            postOrder(2 * index + 1);
        }
        if (2 * index + 2 < arr.length) {
            postOrder(2 * index + 2);
        }
        System.out.println(arr[index]);
    }

}

